package com.wintrue.ffxs.base;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.squareup.picasso.Picasso;
import com.wintrue.ffxs.bean.User;
import com.wintrue.ffxs.config.AppConfig;
import com.wintrue.ffxs.config.PreferenceConfig;
import com.wintrue.ffxs.exception.CrashException;
import com.wintrue.ffxs.http.ApiService;
import com.wintrue.ffxs.http.HttpsFactroy;
import com.wintrue.ffxs.receiver.NetChangeObserver;
import com.wintrue.ffxs.receiver.NetworkStateReceiver;
import com.wintrue.ffxs.ui.NetworkStateActivity;
import com.wintrue.ffxs.utils.FrescoImageLoader;
import com.wintrue.ffxs.utils.ImageDownLoader;
import com.wintrue.ffxs.utils.ImageUtil;
import com.wintrue.ffxs.utils.StringEncodeUtil;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @des:
 * @author:th
 * @email:342592622@qq.com
 * @date: 2017/3/22 14:22
 */

public class MApplication extends android.support.multidex.MultiDexApplication {
    private static final int DEFAULT_TIMEOUT = 20;//网络请求超时时间 秒

    private static MApplication mInstance;
    private String mDisplay;// 屏幕分辨率
    private boolean mIsWifiConnectNetWork;
    private ActivityManager mStackManager;//应用activity管理器
    private boolean mNetworkAvailable = true;
    private ApiService mApiService;
    private User mUser;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        mStackManager = ActivityManager.getInstance();
        initExceptionHandler();
        initNetworkObserver();
        iniApiService(DEFAULT_TIMEOUT);
        initFresco();
        initActivityLife();
        initPicasso();
    }

    private void initFresco() {
        Fresco.initialize(this, FrescoImageLoader.getImagePipelineConfig(this));
    }

    public static MApplication getInstance() {
        return mInstance;
    }

    private void initActivityLife() {
        registerActivityLifecycleCallbacks(new android.app.Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        });
    }

    /**
     * APP是否处于前台唤醒状态
     *
     * @return
     */
    public boolean isAppOnForeground() {
        android.app.ActivityManager activityManager = (android.app.ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = getApplicationContext().getPackageName();
        List<android.app.ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null)
            return false;

        for (android.app.ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals(packageName)
                    && appProcess.importance == android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }

        return false;
    }

    /**
     * 获取App安装包信息
     *
     * @return
     */
    public PackageInfo getPackageInfo() {
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace(System.err);
        }
        if (info == null)
            info = new PackageInfo();
        return info;
    }

    public String getVersion() {
        return getPackageInfo().versionName;
    }

    public int getVersionCode() {
        return getPackageInfo().versionCode;
    }

    /**
     * 获取设备信息
     *
     * @return
     */
    public String getDeviceInfo() {
        String deviceInfo = Build.MANUFACTURER + "#" + Build.VERSION.RELEASE
                + "#android#" + Build.BRAND + "#" + Build.MODEL + "#"
                + getDisplay();
        return deviceInfo;
    }

    public String getDisplay() {
        return mDisplay;
    }

    /**
     * 屏幕分辨率
     *
     * @param mDisplay
     */
    public void setDisplay(String mDisplay) {
        this.mDisplay = mDisplay;
    }

    /**
     * 获取App唯一标识
     *
     * @return
     */
    public String getAppUniqueID() {
        String uniqueID = PreferenceConfig.getAppUniqueID(this);
        if (StringEncodeUtil.isEmpty(uniqueID)) {
            // 获取设备ID
            try {
                TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                uniqueID = tm.getDeviceId();
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 获取不到，则随机生成一个标识
            if (StringEncodeUtil.isEmpty(uniqueID)) {
                uniqueID = UUID.randomUUID().toString();
            }
            PreferenceConfig.saveAppUniqueId(this, uniqueID);
        }
        return uniqueID;
    }

    /**
     * 覆盖系统异常处理
     */
    private void initExceptionHandler() {
        // 注册App异常崩溃处理器
        if (AppConfig.SEND_CARSH) {
            Thread.setDefaultUncaughtExceptionHandler(new CrashException(this));
        }
    }

    public boolean isWifiConnectNetWork() {
        return mIsWifiConnectNetWork;
    }

    public void setWifiConnectNetWork(boolean isWifiConnectNetWork) {
        this.mIsWifiConnectNetWork = isWifiConnectNetWork;
    }

    /**
     * activity管理堆栈
     */
    public ActivityManager getStackManager() {
        if (mStackManager == null) {
            mStackManager = ActivityManager.getInstance();
        }
        return mStackManager;
    }

    /**
     * 注册网路连接状态监听
     */
    private void initNetworkObserver() {
        if (NetworkStateReceiver.getNetworkType(mInstance) == NetChangeObserver.NetType.wifi || NetworkStateReceiver.getNetworkType(mInstance) == NetChangeObserver.NetType.G4) {
            mIsWifiConnectNetWork = true;
        }
        NetworkStateReceiver.registerObserver(new NetChangeObserver() {

            @Override
            public void onConnect(NetType type) {
                super.onConnect(type);
                mNetworkAvailable = true;
                if (mStackManager != null) {
                    Activity activity = mStackManager.getCurrentActivity();
                    if (activity != null
                            && (activity instanceof NetworkStateActivity)) {
                        ((NetworkStateActivity) activity).onConnect(type);
                    }
                }
            }

            @Override
            public void onDisConnect() {
                super.onDisConnect();
                mNetworkAvailable = false;
                if (mStackManager != null) {
                    Activity activity = mStackManager.getCurrentActivity();
                    if (activity != null
                            && (activity instanceof NetworkStateActivity)) {
                        ((NetworkStateActivity) activity).onDisConnect();
                    }
                }
            }
        });
    }

    /**
     * 网络连接状态
     */
    public boolean isNetworkAvailable() {
        return mNetworkAvailable;
    }

    public ApiService iniApiService(int timeOut) {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
//                .addInterceptor(cacheInterceptor)
//                .addNetworkInterceptor(cacheInterceptor)
//                .cache(cache)
                .connectTimeout(timeOut, TimeUnit.SECONDS)
                .writeTimeout(timeOut, TimeUnit.SECONDS)
                .readTimeout(timeOut, TimeUnit.SECONDS)
//                .hostnameVerifier(mHostnameVerifier)
                .sslSocketFactory(HttpsFactroy.getSSLSocketFactory(this))
//                .protocols(Collections.singletonList(Protocol.HTTP_1_1))
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(AppConfig.HTTP_BASE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        mApiService = retrofit.create(ApiService.class);
        return mApiService;
    }

    private void initPicasso(){
       Picasso.setSingletonInstance(new Picasso.Builder(this).
                downloader(new ImageDownLoader(ImageUtil.getUnsafeOkHttpClient(this)))
                .build());
    }

    public ApiService getApiService() {
        return mApiService;
    }

    /**
     * 退出系统
     */
    public void exit() {
        mStackManager.appExit();
    }

    public User getUser() {
        mUser = PreferenceConfig.readUser(this);
        if (mUser == null) {
            mUser = new User();
        }
        return this.mUser;
    }

    public void setUser(User user) {
        PreferenceConfig.saveUser(this, user);
        this.mUser = user;
    }

}
